<template>
  <div class="main">
    <a-card class="w-full h-full">
      <title-table :pageDetail="Data.pageDetail"></title-table>
      <SearchGroup
        :labelOption="Data.labelOption"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
        :searchOption="filterOption"
      />

      <!-- <a-button type="primary" @click="handleExport">导出异常信息</a-button> -->
      <a-table
        :dataSource="list"
        :columns="createColumns()"
        bordered
        bodyCell
        :loading="loading"
        :scroll="{ x: 1450 }"
        size="small"
        :pagination="false"
        :row-class-name="
          (record, index) => {
            if (!record.confirmYcFlag) return 'bg-red-200'
            if (record.confirmYcFlag && record.ycZt !== 'YC_YCL') return 'bg-orange-200'
            if (record.confirmYcFlag && record.ycZt === 'YC_YCL') return 'bg-green-200'
          }
        "
      >
        <template #bodyCell="{ column, index, record }">
          <a-button
            type="link"
            @click="goDetailPage(record)"
            :danger="!record.confirmYcFlag"
            v-if="column.key === 'handle'"
          >
            {{ !record.confirmYcFlag ? '立即处理' : '查看详情' }}
          </a-button>
          <template v-if="column.key == 'index'">
            <span>
              {{ (curPage - 1) * pageSize + (index + 1) }}
            </span>
          </template>
        </template>
      </a-table>
      <a-pagination
        :show-total="total => `共 ${total} 条`"
        :show-size-changer="false"
        show-quick-jumper
        v-model:current="curPage"
        v-model:pageSize="pageSize"
        :total="total"
      />
    </a-card>
  </div>
</template>

<script setup lang="ts" name="AbnormalExaminaPaper">
import { onMounted, computed, reactive, onActivated, unref, ref } from 'vue'
import { useRouter } from 'vue-router'
import { getAuthCache, setAuthCache } from '/@/utils/auth'
import { getYcztConfigList } from '/@/api/system-manage/teacher-info'
import { useMessage } from '/@/hooks/web/useMessage'
const { createMessage } = useMessage()

onActivated(async () => {
  let zpDataFlag = getAuthCache('zpDataFlag')
  if (zpDataFlag) {
    defaultFilterOption['zpDataFlag'] = filterOption.value['zpDataFlag'] = zpDataFlag
    setAuthCache('zpDataFlag', '')
    reset()
  }
  if (filterOption.value['zpDataFlag'] && !zpDataFlag) {
    loadData()
  }
  getDetail()
})

onMounted(async () => {
  const { data, code } = await getYcztConfigList({})
  if (code == 200) {
    data.forEach(ele => {
      ele.label = ele.ycName
      ele.value = ele.autoId
    })
    ycConfigList.value = [{ label: '全部', value: '' }, ...data]
  }
  if (userInfo?.value?.manageKmdm && userInfo.value?.manageKmdm != '0') {
    await getKmpyJd(userInfo.value.manageKmdm, true)
    defaultFilterOption['kmdm'] = filterOption.value['kmdm'] = userInfo.value?.manageKmdm
  } else {
    getKmList()
  }
})

import {
  createColumns,
  ycActionList,
} from '/@/views/examination-audit/abnormal-examination-paper/components/index'

import titleTable from './components/titleTable.vue'

import { getAbnormityPaper, getAcountmdm } from '/@/api/examinationAudit'
import { getKmSimpAlleList } from '/@/api/organization/subjectManage'

import { useList } from '/@/hooks/web/useList'
import { useUserStore } from '/@/store/modules/user'
import { getYcAboutInfo } from '/@/api/teacher'

const router = useRouter()
const userStore = useUserStore()
const kmListOption = ref<any[]>([])

const ycConfigList = ref<any[]>([])

const userInfo: any = computed(() => userStore.getUserInfo)
const authorityInfo = computed(() => userStore.getAuthorityItem)
import { selectKmpyjdOption } from '/@/views/home/index/index'
import { ycZtList } from '/@/views/examination-audit/abnormal-examination-paper/components/index'

const Data = reactive({
  labelOption: [
    {
      name: '评阅阶段',
      key: 'zpDataFlag',
      labelWidth: 80,
      selectOption: selectKmpyjdOption,
      type: 'Radio',
      inputWidth: 180,
      isNotReset: true,
    },
    {
      labelWidth: 50,
      key: 'kmdm',
      disabled: userInfo?.value?.manageKmdm && userInfo.value?.manageKmdm != '0',
      type: 'Select',
      name: '科目',
      selectOption: kmListOption,
      fieldNames: { label: 'kmmc', value: 'kmdm' },
    },
    {
      name: '异常状态',
      key: 'ycZt',
      labelWidth: 80,
      selectOption: [{ label: '全部', value: '' }, ...unref(ycZtList)],
      inputWidth: 180,
    },
    {
      name: '证实状态',
      key: 'confirmYcFlag',
      labelWidth: 80,
      selectOption: [
        { label: '全部', value: '' },
        { label: '已证实', value: 'true' },
        { label: '待证实', value: 'false' },
      ],
      inputWidth: 180,
    },
    {
      name: '异常类型',
      key: 'ycConfigId',
      labelWidth: 80,
      selectOption: ycConfigList,
      inputWidth: 180,
    },
    {
      name: '证实类型',
      key: 'confirmYcAction',
      labelWidth: 80,
      selectOption: [{ label: '全部', value: '' }, ...unref(ycActionList)],
      inputWidth: 180,
    },
  ],
  pageDetail: {},
} as any)

const {
  defaultFilterOption,
  reset,
  loadData,
  filterOption,
  curPage,
  total,
  pageSize,
  list,
  loading,
} = useList(getAbnormityPaper, {
  defaultFilterOption: <any>{
    pyjsFlag: authorityInfo.value?.roleCode == 'PYJS' ? true : false,
  },
  immediate: false,
})

async function getKmpyJd(kmdm, isDefault = false) {
  if (!kmdm || kmdm === '0') return
  const { code, data } = await getAcountmdm({ kmdm })
  if (code == 200) {
    !defaultFilterOption['zpDataFlag'] &&
      (defaultFilterOption['zpDataFlag'] = filterOption.value['zpDataFlag'] =
        data.kmpyjd === 'FORMAL' ? 'true' : 'false')
    isDefault &&
      (kmListOption.value = [
        {
          kmmc: `${data.kmdm}(${data.kmmc})`,
          kmdm: data.kmdm,
        },
      ])
  }
}

async function getKmList() {
  let { code, data } = await getKmSimpAlleList({
    pageSize: 0,
  })
  if (code == 200) {
    data.forEach(item => (item.kmmc = `${item.kmdm}(${item.kmmc})`))
    kmListOption.value = data.sort((a, b) => a.kmdm - b.kmdm)
  }
}

const goDetailPage = record => {
  router.push({
    name: 'ReviewPaper',
    query: {
      ycId: record.ycId,
      pyJd: filterOption.value['zpDataFlag'] == 'true' ? 'FORMAL' : 'TEST',
    },
  })
}

// 异常信息统计=
const getDetail = async () => {
  if (!filterOption.value['zpDataFlag']) return
  let { code, data } = await getYcAboutInfo({
    zpDataFlag: filterOption.value['zpDataFlag'],
    kmdm: filterOption.value['kmdm'],
  })
  if (code == 200) {
    Data.pageDetail = data || {}
  }
}

async function handleSearch() {
  if (!filterOption.value['zpDataFlag']) return createMessage.error('请选择评阅阶段')
  await getKmpyJd(filterOption.value.kmdm)
  getDetail()
  loadData(1)
}
function handleReset(NotReset: any = {}) {
  if (!filterOption.value['zpDataFlag']) return
  reset(NotReset)
  getDetail()
}
</script>

<style lang="less" scoped>
.main {
  :global(.ant-pagination) {
    margin-top: 20px !important;
  }
}
</style>
